$(".tabBarBox").load("../../common-html/resource-topTab.html");
var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
var code = ''
$("#beijing").attr("checked", true);
$("#tianjin").attr("checked", true);
$("#zhengzhou").attr("checked", true);
$("#jinan").attr("checked", true);
$("#xian").attr("checked", true);

$("#form-member").validate({
    rules: {
        username: "titleCheck",
    },
    //提交表单数据
    submitHandler: function (form) {
        queryDataFn();
    }
})

function queryDataFn() {
    if ($("#category_id").val() == "") {
        console.log($("#category_id").val())
        layer.msg('请选择配置内容!', {icon: 6, time: 1000});
        return;
    }
    //获取
    var category_id = $("#category_id").val(),
        logmax = $("#logmax").val(),
        logmin = $("#logmin").val(),
        shaixuan = $("#shaixuan").val(),
        text = $("#text").val(),
        config_location = $("#kong").find("option:selected").val(),
        pingtai = $("#pingtai").find("option:selected").val(),
        cangku = code
    //提交
    var user_data = {
        category_id: category_id,
        created_at_end: logmax,
        created_at_start: logmin,
        status: shaixuan,
        title: text,
        config_location: config_location,
        platform: pingtai,
        warehouse_code: cangku,
        different_customer_code: $("#kehu").find("option:selected").val()
    }
    console.log(user_data)
    //获取返回的数据
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            var dataSource = new Array();
            dataSource = [];
            var result = data.data
            $.each(result, function (i, field) {
                var rowData = new Array();
                rowData.push(1);
                rowData.push(field.platform);

                rowData.push(field.different_customer_code);

                rowData.push(field.warehouse_name);
                //活动标题-0
                rowData.push(field.config_location);
                //标题-1
                rowData.push(field.title);
                //生效时间-2
                rowData.push(field.started_at);
                //失效时间-3
                rowData.push(field.end_in);
                //状态-4
                rowData.push(field.status);
                //创建人-5
                rowData.push(field.created_name);
                //创建时间-6
                rowData.push(field.created_at);
                //类目id-7
                rowData.push(field.category_id);
                //活动id-8
                rowData.push(field.resource_id);
                //url地址-9
                rowData.push(field.image_url);
                //10-色值
                rowData.push(field.color_value);
                //11-
                rowData.push(field.image_path);

                rowData.push(field.warehouse_code);
                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            return false
        } else {
            var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    return web.doAjax(root.sroreinter.search_resource_conf, 'post', user_data, succCallBack);

    //获取数据之后如何展现在页面上
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback": function () {
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                this.api().column(0).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                    //权限控制函数
                    power()
                });
            },
            "autoWidth": false,
            "order": [[10, "desc"]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "scrollY": "300px",
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [10, 20, 30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {"data": null, "targets": 0},
                {
                    "render": function (data, type, row, meta) {
                        var val
                        if (row[1] == '10,20') {
                            val = '普通平台/签约平台'
                        } else if (row[1] == '20') {
                            val = '普通平台'
                        } else {
                            val = '签约平台'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[1].indexOf("10") >= 0) {
                            if (row[2] == 10) {
                                val = '分销客户'
                            } else if (row[2] == '20') {
                                val = '非分销客户'
                            } else if (row[2] == '10,20') {
                                val = '分销客户/非分销客户'
                            }
                        }

                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[6];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[7];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[10];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 10
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '<a class="powerButton 457"><i class="Hui-iconfont mr-10" onclick="edit_data(' + row[12] + ')">&#xe6df;</i></a> <a class="powerButton 458"><i class="Hui-iconfont" onclick="delete_data(' + row[12] + ')">&#xe6e2;</i></a>';
                        return val;
                    },
                    "targets": 11
                },
            ],
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    };
}

function changeCang() {
    code = ''
    var warcode = ""
    var warehouses = JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data, function (k, s) {
        if (document.getElementById('yun' + s.warehouseCode + '').checked == true) {//获取选中的id
            warcode += document.getElementById('yun' + s.warehouseCode + '').value + ","//逗号拼接获取到的所以id
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    console.log(warcode)
    code = warcode

}

function delete_data(id) {
    layer.confirm('你确定要删除吗？', function (index) {
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_resource_conf,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                resource_id: id,
            },
            //传入组装的参数
            success: function (data, user_data, succCallBack) {
                console.log(data)
                if (data.errorCode == "0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout(function () {
                        isEdit = true;
                        queryDataFn();
                    }, 700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        })
    });
}
function edit_data(id) {
    var index = layer.open({
        type: 2,
        title: "通栏编辑配置",
        content: "top_edit.html" + "?id=" + id
    });
    layer.full(index);
}

/*
配置内容下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id: 1, type: 101
        },  //传入组装的参数
        success: function (data) {
            if (data.errorCode == "0") {
                var dataInter = data.data;
                var interText = doT.template($("#ascription_bob").text());
                $.each(dataInter, function (inx, data2) {
                    if (data2.name == "通栏配置") {
                        var member = '<option selected value="' + data2.category_id + '" >' + data2.name + '</option>';
                        $('#category_id').append(member)
                    } else {
                        var member = '<option value="' + data2.category_id + '" >' + data2.name + '</option>';
                        $('#category_id').append(member)
                    }
                })

            }
        }
    })
})

function discount() {
    $("#category_id").change(function (event) {
        var fanx = $(this).find("option:selected");
        if (fanx.val() == 3) {
            window.location.href = "../resource-html/resource.html?value=" + "拉幕配置"
        }
        if (fanx.val() == 4) {
            window.location.href = "../resource-html/resource.html?value=" + "浮框配置"
        }
        if (fanx.val() == 5) {
            window.location.href = "../resource-html/resource.html?value=" + "弹框配置"
        }
        //轮播图配置
        if (fanx.val() == 6) {
            window.location.href = "../picture-html/picture.html"
        }
        //搜索栏配置
        if (fanx.val() == 7) {
            window.location.href = "../search-html/search.html"
        }
        //品类展示区配置
        if (fanx.val() == 18) {
            window.location.href = "../display-html/display.html"
        }
        //楼层配置
        if (fanx.val() == 10) {
            // window.location.href = "../floor-html/floor.html"
            window.location.href = "../floorManagement-html/floorManagement.html"
        }
        if (fanx.val() == 21) {
            window.location.href = "../categorylogo-html/categorylogo.html"
        }
        if (fanx.val() == 22) {
            window.location.href = "../brandhalllogo-html/brandhalllogo.html"
        }
    });
}